// BbsLayer.cpp: implementation of the CBbsLayer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AnsiTerminal.h"
#include "BbsLayer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBbsLayer::CBbsLayer(CTelnetLayer *pTelnetLayer, CAnsiScreen *pScreen, IVirtualKeyboard *pKeyboard) :
	m_stateCol(pTelnetLayer, pScreen, pKeyboard),
	m_mouseSensitiveCol(pScreen)
{
	m_pTelnetLayer = pTelnetLayer;
	ASSERT(pScreen);
	m_pScreen = pScreen;
	m_screenChangeCounter = -1;
	theApp.RegisterIdleListener(this);
	m_bMouseFreezed = FALSE;
}

CBbsLayer::~CBbsLayer()
{

}

void CBbsLayer::updateState()
{
	if(m_screenChangeCounter != m_pScreen->GetChangeCounter())
	{
		m_screenChangeCounter = m_pScreen->GetChangeCounter();
		m_stateCol.UpdateActiveState(m_lastCursorCell);
	}
}

BOOL CBbsLayer::OnMouseDown(CPoint ptCell, UINT nFlags)
{
	updateState();
	return m_stateCol.GetActiveState()->OnMouseDown(ptCell, nFlags);
}

BOOL CBbsLayer::OnMouseMove(CPoint ptCell, UINT nFlags)
{
	m_lastCursorCell = ptCell;

	m_mouseSensitiveCol.UpdateActiveSensitive(ptCell);
	
	updateState();
	return m_stateCol.GetActiveState()->OnMouseMove(ptCell, nFlags);
	
}

BOOL CBbsLayer::OnMouseUp(CPoint ptCell, UINT nFlags)
{
	m_mouseSensitiveCol.UpdateActiveSensitive(ptCell);
	if(m_mouseSensitiveCol.GetActiveSensitive()->Click(m_pScreen, ptCell, nFlags))
	{
		return TRUE;
	}
	updateState();
	return m_stateCol.GetActiveState()->OnMouseUp(ptCell, nFlags);
}

void CBbsLayer::OnAppIdle()
{
	if(m_bMouseFreezed)
	{
		return;
	}
	updateState();
	m_mouseSensitiveCol.UpdateActiveSensitive(m_lastCursorCell);
}

BOOL CBbsLayer::OnMouseWheel(CPoint ptCell, UINT nFlags, short wheelNotches)
{
	updateState();
	return m_stateCol.GetActiveState()->OnMouseWheel(ptCell, nFlags, wheelNotches);
}

BOOL CBbsLayer::OnFreezeMouse(BOOL bFreeze)
{
	if(bFreeze)
	{
		m_mouseSensitiveCol.SetNullMouseSensitive();
		m_stateCol.SetNullBbsState();
	}
	else
	{
		m_mouseSensitiveCol.UpdateActiveSensitive(m_lastCursorCell);
		m_stateCol.UpdateActiveState(m_lastCursorCell);
	}
	m_bMouseFreezed = bFreeze;
	return TRUE;
}